import 'package:flutter/material.dart';
import 'package:flutterapp/common/util/log_utils.dart';
import 'package:flutterapp/common/util/theme_data_utils.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../common/util/sp_utils.dart';

/// 全局数据模型类，需要在启动时放入
///
/// 放置一些全局的数据，比如包数据，主题数据等
class GLobalAppModel extends ChangeNotifier {
  /// 当前主题
  ThemeData? curTheme;

  /// 当前包的信息
  PackageInfo? packageInfo;

  GLobalAppModel() {
    init();
  }

  init() {
    initCurTheme();
    initPackageInfo();
  }

  /// 初始化主题数据
  Future<bool> initCurTheme() {
    return SPUtils.getInstance().getThemeIndex().then((index) {
      curTheme = ThemeDataUtils.getThemeByIndex(index ?? 0);
      notifyListeners();
      LogUtils.d('获取到的主题序号是:$index');
      return true;
    });
  }

  Future<bool> initPackageInfo() {
    return PackageInfo.fromPlatform().then((value) {
      packageInfo = value;
      return true;
    });
  }

  /// 刷新当前主题
  void refreshCurTheme(int position) {
    curTheme = ThemeDataUtils.getThemeByIndex(position);
    notifyListeners();
  }
}
